import { http } from '@renderer/plugins/axios'
import useUtil from './useUtil'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'

export default () => {
  const calculateInfo = ref<CalculateModel>()
  const calculateSetInfo = ref<CalculateSetInfoModel>()

  const templateList = ref<TemplateModel[]>()
  const getFloorGather = useUtil().request(async (params) => {
    try {
      return http.request({
        url: '/getFloorGather',
        method: 'GET',
        params,
      })
    } catch (error) {
      return null
    }
  })
  const getOtherGather = useUtil().request(async (params) => {
    try {
      return http.request({
        url: '/getOtherGather',
        method: 'GET',
        params,
      })
    } catch (error) {
      return null
    }
  })
  const getCarpentryGather = useUtil().request(async (params) => {
    try {
      return http.request({
        url: '/getCarpentryGather',
        method: 'GET',
        params,
      })
    } catch (error) {
      return null
    }
  })
  const getWardrobeGather = useUtil().request(async (params) => {
    try {
      return http.request({
        url: '/getWardrobeGather',
        method: 'GET',
        params,
      })
    } catch (error) {
      return null
    }
  })
  const getHouseGather = useUtil().request(async (params) => {
    try {
      return http.request({
        url: '/getHouseGather',
        method: 'GET',
        params,
      })
    } catch (error) {
      return null
    }
  })
  const submitGather = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/submitGather',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '提交核算成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const reGather = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/reGather',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '重新核算成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getCalculateDetail = useUtil().request(async (params) => {
    try {
      calculateInfo.value = await http.request({
        url: '/getCalculateDetail',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const saveTableData = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/gui/saveTableData',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '保存成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const updateCostFloatData = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/updateCostFloatData',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '修改成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getCalculateTableSet = useUtil().request(async (params) => {
    try {
      calculateSetInfo.value = await http.request({
        url: '/getCalculateTableSet',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const delTableData = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/gui/delTableData',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '删除成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const updateWardrobeBudgetRemark = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/updateWardrobeBudgetRemark',
        method: 'POST',
        data,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const updateMainBudgetCate = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/updateMainBudgetCate',
        method: 'POST',
        data,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const changeBudgetDataSort = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/changeBudgetDataSort',
        method: 'POST',
        data,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const getCalculateExportData = useUtil().request(async (params) => {
    try {
      templateList.value = await http.request({
        url: '/gui/getCalculateExportData',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const getCalculateGatherNumber = useUtil().request(async (params) => {
    try {
      const res = await http.request({
        url: '/getCalculateGatherNumber',
        method: 'GET',
        params,
      })
      return res
    } catch (error) {
      return null
    }
  })
  return {
    getFloorGather,
    getOtherGather,
    getCarpentryGather,
    getWardrobeGather,
    getHouseGather,
    submitGather,
    getCalculateDetail,
    calculateInfo,
    reGather,
    saveTableData,
    updateCostFloatData,
    getCalculateTableSet,
    calculateSetInfo,
    delTableData,
    getCalculateExportData,
    templateList,
    getCalculateGatherNumber,
    changeBudgetDataSort,
    updateWardrobeBudgetRemark,
    updateMainBudgetCate,
  }
}
